package com.tedu.ivos.geofence.service.impl;

import com.tedu.ivos.base.exception.ServiceException;
import com.tedu.ivos.base.response.JsonResult;
import com.tedu.ivos.base.response.StatusCode;
import com.tedu.ivos.geofence.mapper.GeofenceMapper;
import com.tedu.ivos.geofence.pojo.dto.GeofenceParam;
import com.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import com.tedu.ivos.geofence.pojo.entity.Geofence;
import com.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import com.tedu.ivos.geofence.service.IGeofenceService;
import com.tedu.ivos.vehicle.mapper.VehicleMapper;
import com.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import com.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements IGeofenceService {

    @Autowired
    GeofenceMapper geofenceMapper;

    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> select(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏业务，参数：{}",geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.select(geofenceQuery);
        //之前没有对围栏的查了数据进行处理，现在与需要对数据进行补充：遍历每一个围栏，依次为每个围栏补充车辆数据(VO中预留的)
        for (int i=0;i<list.size();i++) {
            GeofenceVO geofenceVO = list.get(i);
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            geofenceVO.setTotalNum(vehicleVOList.size());
            geofenceVO.setVehicleList(vehicleVOList);
            int availableNum = 0;
            for (VehicleVO vehicleVO : vehicleVOList) {
                if (vehicleVO.getStatus().equals("1")) {
                    availableNum++;
                }
            }
            geofenceVO.setAvailableNum(availableNum);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("电子围栏业务，参数：{}",geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        //记得进行判定
        if(geofence.getId()!=null){
            geofence.setStatus(geofenceParam.getStatus());
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
            log.debug("修改电子围栏业务，参数：{}",geofenceParam);
        }else{
            //TODO 新增电子围栏
            geofence.setCreateTime(new Date());
            geofence.setStatus("0");
            geofenceMapper.insert(geofence);
            log.debug("新增电子围栏业务，参数：{}",geofenceParam);
        }

    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除电子围栏业务，参数：{}",id);
        //业务：需要判断当前围栏上是否有已绑定的车辆，如果有绑定车辆就不能删除
        /*1.封装车辆查询的DTO层对象VehicleQuery 是为了将围栏Id传给车辆的geofenceId
            这样才能调用vehicleMapper的selectVehicle(vehicleQuery)方法
         */
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        //2.调用车辆数据层查询是否有绑定再当前围栏上的车辆数据
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //3.如果当前围栏上有绑定车辆，业务层可以抛出操作失败的异常 非null判断多注意
        if(list.size()>0 && list!=null){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }else {
            //4.如果当前围栏上无绑定车辆则可删除
            geofenceMapper.delete(id);
        }
    }
}
